package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.annotation.LoginUser;
import io.renren.bean.ExamInfo;
import io.renren.bean.ExamQuestionInfo;
import io.renren.bean.ExamResult;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.entity.ExamEntity;
import io.renren.entity.StudentEntity;
import io.renren.service.*;
import io.renren.util.ApiBaseAction;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * 考试模块
 *
 * @author huolikang
 */
@RestController
@RequestMapping("api/exam")
public class ApiExamController extends ApiBaseAction {

    @Autowired
    private ExamDatailsService examDatailsService;
    @Autowired
    private ExamTemplateService examTemplateService;
    @Autowired
    private ExamService examService;
    @Autowired
    private StuSubService stuSubService;
    @Autowired
    private QuestionService questionService;

    /**
     * 获取考试信息
     *
     * @param student   学员
     * @param subjectId 科目id
     * @return R
     * @author huolikang
     * @date 2018年10月30日
     */
    @Login
    @PostMapping("getExamInfo")
    @ApiOperation("获取考试信息")
    public R getExamInfo(@LoginUser StudentEntity student,
                         @RequestParam("subjectId") Integer subjectId) {
        ExamInfo examInfo = examTemplateService.getExamInfo(student.getUserId(), subjectId);
        return R.ok(examInfo);
    }

    /**
     * 获取考试记录
     *
     * @param student   学员
     * @param subjectId 科目id
     * @param page      默认第一页
     * @param size      默认一页10条数据
     * @return R
     * @author huolikang
     * @date 2018年10月30日
     */
    @Login
    @PostMapping("getExamList")
    @ApiOperation("获取考试记录")
    public R getExamList(@LoginUser StudentEntity student,
                         @RequestParam("subjectId") Integer subjectId,
                         @RequestParam Map<String, Object> params,
                         @RequestParam(value = "page", defaultValue = "1") Integer page,
                         @RequestParam(value = "size", defaultValue = "10") int size) {
        params.put("studentId", student.getUserId());
        params.put("subjectId", subjectId);
        //分页获取考试记录
        PageUtils getExamList = examService.queryPage(params, page, size);
        return R.ok(getExamList);
    }

    /**
     * 创建试卷
     *
     * @param student   学员
     * @param subjectId 科目id
     * @return R
     * @author huolikang
     * @date 2018年10月31日
     */
    @Login
    @PostMapping("createExamPaper")
    @ApiOperation("创建试卷")
    public R createExamPaper(@LoginUser StudentEntity student,
                             @RequestParam("subjectId") Integer subjectId) {
        return examService.createExamPaper(student, subjectId);
    }

    /***
     * 获取考试题目
     *
     * @param student 学员
     * @param examId 试卷id
     * @return R
     */
    @Login
    @PostMapping("getExamQuestionInfo")
    @ApiOperation("获取考试题目")
    public R getExamQuestionInfo(@LoginUser StudentEntity student,
                                 @RequestParam("examId") Integer examId) {
        ExamQuestionInfo examQuestionInfo = examService.getExamQuestionInfo(examId, student.getUserId());
        return R.ok(examQuestionInfo);
    }

    /**
     * 提交某一道题的答案
     *
     * @param @param student.getUserId() 学员id
     * @param @param examId 考试id
     * @param @param questionId 问题id
     * @param @param sort 序号
     * @param @param remainExamSecond 考试剩下时间（秒）
     * @param @param studentAnswer 学员答案
     * @return R
     * @author huolikang
     * @date 2018年11月1日
     */
    @Login
    @PostMapping("submitQuestion")
    @ApiOperation("提交某一道题答案")
    public R submitQuestion(@LoginUser StudentEntity student,
                            @RequestParam("examId") Integer examId,
                            @RequestParam("questionId") Integer questionId,
                            @RequestParam("sort") Integer sort,
                            @RequestParam("remainExamSecond") Integer remainExamSecond,
                            String studentAnswer) {
        //提交答案
        examService.submitQuestion(examId, questionId, sort, remainExamSecond, studentAnswer);
        return R.ok();
    }

    /**
     * 学员提交试卷
     *
     * @param examId    考试id
     * @param subjectId 科目id
     * @param student   学员
     * @return R
     * @author huolikang
     * @date 2018年11月2日
     */
    @Login
    @PostMapping("submitExam")
    @ApiOperation("学员提交试卷")
    public R submitExam(@LoginUser StudentEntity student,
                        @RequestParam("subjectId") Integer subjectId,
                        @RequestParam("examId") Integer examId) {
        ExamEntity exam = examService.selectById(examId);
        if (exam.getState() == 3) {
            return R.fail("你已经提交该试卷了");
        }
        ExamResult examResult = examService.submitExam(examId, subjectId, student.getUserId());
        return R.ok(examResult);
    }

    /***
     * 查看题目的状态(是否提交)
     * @param student 学员
     * @param sort 序号
     * @param examId 试卷id
     * @return R
     */
    @Login
    @PostMapping("questionState")
    @ApiOperation("查看题目状态")
    public R questionState(@LoginUser StudentEntity student, Integer sort, Integer examId) {
        List<Map<String, Object>> isQuestion = examDatailsService.getIsQuestion(student.getUserId(), sort, examId);
        return R.ok(isQuestion);
    }

    /***
     * 根据试卷题目 一题一题获取题目
     * @param student 学员
     * @param sort 序号
     * @param examId 试卷id
     * @return R
     */
    @Login
    @PostMapping("questionOneByOne")
    @ApiOperation("一题一题获取题目")
    public R questionOneByOne(@LoginUser StudentEntity student, @RequestParam("sort") Integer sort, @RequestParam("examId") Integer examId) {
        Map<String, Object> getQuestionOneByOne = examDatailsService.getQuestionOneByOne(student.getUserId(), sort, examId);
        return R.ok(getQuestionOneByOne);
    }
}
